import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router'
import type {App} from 'vue';
import {constantRoutes} from "./routers"
import {createRouterGuard} from "@/router/guard";

function initRoute() {
    let routes: RouteRecordRaw[] = []

    const routesContext: any = import.meta.glob('./module/*.ts', {eager: true})
    Object.keys(routesContext).forEach((v) => {
        routes.push(routesContext[v].default)
    })
    routes.push(...constantRoutes)

    routes = routes.flat()
    return routes
}


export const router = createRouter({
    history: createWebHashHistory(),
    routes: initRoute(),
})


/** Setup Vue Router */
export async function setupRouter(app: App) {
    app.use(router);
    createRouterGuard(router);
    await router.isReady();
}

export default router;

